package com.zhuanzhuan.hero.user.facade;

import com.bj58.spat.scf.server.contract.annotation.ServiceBehavior;
import com.bj58.zhuanzhuan.zzentry.common.service.entity.ServiceHeaderEntity;
import com.bj58.zhuanzhuan.zzentry.common.service.entity.ZZOpenScfBaseResult;
import com.bj58.zhuanzhuan.zzentry.common.service.model.openEntry.ZZHeader;
import com.bj58.zhuanzhuan.zzentry.common.service.model.openEntry.ZZMethod;
import com.bj58.zhuanzhuan.zzentry.common.service.model.openEntry.ZZParam;
import com.zhuanzhuan.hero.user.consts.NumConsts;
import com.zhuanzhuan.hero.user.dto.SavePaymentDTO;
import com.zhuanzhuan.hero.user.enums.ExceptionMsgEnum;
import com.zhuanzhuan.hero.user.enums.PaymentTypeEnum;
import com.zhuanzhuan.hero.user.helper.ZzLockHelper;
import com.zhuanzhuan.hero.user.service.HeroUserInfoService;
import com.zhuanzhuan.hero.user.service.HeroUserPaymentService;
import com.zhuanzhuan.hero.user.util.ServiceHeaderUtil;
import com.zhuanzhuan.hero.user.vo.UserPaymentVO;
import com.zhuanzhuan.zzlock.lock.ZZEtcdLock;
import lombok.extern.slf4j.Slf4j;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * @ClassName UserPaymentFacadeImpl
 * @Description
 * @Author hanjunjun
 * @Date 2024/6/21 16:01
 */
@ServiceBehavior
@Slf4j
public class UserPaymentFacadeImpl implements IUserPaymentFacade {

    @Resource
    private HeroUserPaymentService userPaymentService;

    @Resource
    private ZzLockHelper lockHelper;

    @Resource
    private HeroUserInfoService userInfoService;

    @ZZMethod(value = "saveUserPayment", desc = "保存收款方式", publicAccess = false)
    @Override
    public ZZOpenScfBaseResult<Boolean> saveUserPayment(@ZZHeader ServiceHeaderEntity header,
        @ZZParam("param") @Valid SavePaymentDTO savePaymentDTO) {
        //银行卡号最长20位
        if (PaymentTypeEnum.BANK_CARD.getCode().equals(savePaymentDTO.getPaymentType())
            && savePaymentDTO.getPayCode().length() > NumConsts.TWENTY) {
            throw new IllegalArgumentException("银行卡号不得超出20位数字");
        }

        Long uid = ServiceHeaderUtil.parseUid(header);

        userInfoService.checkCanEdit(uid);

        try (ZZEtcdLock lock = lockHelper.getLock("saveOrUpdateUserPayment_" + uid)) {
            if (!lock.tryLock()) {
                log.info("获取锁失败");
                return ZZOpenScfBaseResult.buildErr(ExceptionMsgEnum.COMMON_ERROR.getCode(), ExceptionMsgEnum.COMMON_ERROR.getErrorMsg());
            }
            userPaymentService.saveOrUpdateUserPayment(uid, savePaymentDTO);
        }
        return ZZOpenScfBaseResult.buildSucc(true);
    }

    @ZZMethod(value = "queryUserPaymentInfo", desc = "查询收款方式", publicAccess = false)
    @Override
    public ZZOpenScfBaseResult<List<UserPaymentVO>> queryUserPaymentInfo(@ZZHeader ServiceHeaderEntity header) {
        return ZZOpenScfBaseResult.buildSucc(userPaymentService.queryUserPaymentInfoByUid(ServiceHeaderUtil.parseUid(header)));
    }

    @ZZMethod(value = "queryUserDefaultPaymentInfo", desc = "查询默认收款方式", publicAccess = false)
    @Override
    public ZZOpenScfBaseResult<UserPaymentVO> queryUserDefaultPaymentInfo(@ZZHeader ServiceHeaderEntity header) {
        return ZZOpenScfBaseResult.buildSucc(userPaymentService.queryUserDefaultPaymentInfo(ServiceHeaderUtil.parseUid(header)));
    }
}
